package com.main.ok.nio;

import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.RandomAccessFile;
import java.nio.ByteBuffer;
import java.nio.channels.FileChannel;

/**
 * Created by kai on 2015/6/11 0011.
 * 读取文件 使用fileChannel
 */
public class ReadFile {
    public static void main(String[] args) throws IOException {
        //test channel
        RandomAccessFile randomAccessFile = new RandomAccessFile("e:/test.txt", "rw");
        FileChannel channel = randomAccessFile.getChannel();
        ByteBuffer buffer = ByteBuffer.allocate(4);
        int readByte = channel.read(buffer);
//        System.out.println("readbyte:" + readByte);
        while (readByte != -1) {
//            System.out.println("read:" + readByte);
            buffer.flip();
            while (buffer.hasRemaining()) {
                System.out.print((char) buffer.get());
            }
            buffer.clear();
            readByte = channel.read(buffer);
        }
        channel.close();
    }
}
